/**
 * zoneland.net Inc.
 * Copyright (c) 2002-2013 All Rights Reserved.
 */
package com.sanerzone.smscenter.gateway.util;


import com.sanerzone.smscenter.gateway.ums.comm.cmpp.CMPPSMProxy;
import com.sanerzone.smscenter.gateway.ums.comm.cmpp.message.CMPPSubmitMessage;
import com.sanerzone.smscenter.gateway.ums.comm.cmpp.message.CMPPSubmitRepMessage;
import com.sanerzone.smscenter.gateway.ums.comm.sgip.message.SGIPSubmitRepMessage;

import java.util.concurrent.Callable;

/**
 * 
 * @author gang
 * @version $Id: SendThread.java, v 0.1 2013-4-12 上午10:59:30 gang Exp $
 */
public class SendThread implements Callable<Boolean> {


    private CMPPSubmitMessage msg;
    private CMPPSMProxy cmppProxy;

    public SendThread(CMPPSMProxy cmppProxy, CMPPSubmitMessage msg) {
        this.msg = msg;
        this.cmppProxy = cmppProxy;
    }

    /** 
     * @see java.util.concurrent.Callable#call()
     */
    public Boolean call() throws Exception {
        return doSend(msg);
    }

    public Boolean doSend(CMPPSubmitMessage msg) throws Exception {
        //监控线程的执行时间，对于超时的线程进行中断，默认8秒超时
        ThreadMonitor m = new ThreadMonitor(Thread.currentThread());
        m.start();
        try {
            SGIPSubmitRepMessage a = null;
        	CMPPSubmitRepMessage rep = (CMPPSubmitRepMessage) cmppProxy.send(msg);
        	if(rep.getResult() == 0) {
        		CmppT.cnt.incrementAndGet();
//        		rep.getSubmitSequenceNumberStr();
                s(bytes2Long(rep.getMsgId()));
                System.out.println("--------------"+ rep.getMsgIdStr()+","+rep.getMsgId()+"," +rep.getCommandId() + "," + rep.getResult() +"--------------------");
                System.out.println("----------22222----"+rep.getMsgIdStr()+"------------2222--------");

                return true;
        	}
        	
            return false;
        } finally {
            m.stop();
        }

    }


    public void  s(long temp) {
        byte[] bt = new byte[8];
        for(int i=0;i<8;i++){
            bt[i] = (byte) (temp >> (56 - i * 8));
        }

        int mask = 0xf0;
        int month =  (bt[0] & mask)>>4;
        System.out.println("Month:"+month);

        mask=0xF;
        int date =  (bt[0] & mask)<<1;
        mask=0x80;
        int date2 = bt[1]>>7;
        date = (date&(date2|0x1E));
        System.out.println("Date:"+date);

        //01111100
        mask=0x7C;
        int hour = (bt[1]&mask)>>2;
        System.out.println("Hour:"+hour);

        mask=0x3;
        int minute = (bt[1]&mask)<<4;
        minute |= (bt[2]&0xF0)>>4;
        System.out.println("Minute:"+minute);

        mask=0xf;
        int second = (bt[2]&mask)<<2;
        second |= (bt[3]&0xff)>>6;
        System.out.println("Second:"+second);

        mask = 0x3F;
        int Msg_Id = (bt[3]&mask)<<16;
        Msg_Id |= (bt[4]&0xFF)<<8;
        Msg_Id |= (bt[5]&0xFF);
        System.out.println("Msg_Id:"+Msg_Id);

        mask = 0xFF;
        int seq_id = (bt[6]&mask)<<8;
        seq_id |= (bt[7]&mask);
        System.out.println("seq_id:"+seq_id);
    }
    public static long bytes2Long(byte[] byteNum) {
        long num = 0;
        for (int ix = 0; ix < 8; ++ix) {
            num <<= 8;
            num |= (byteNum[ix] & 0xff);
        }
        return num;
    }
}
